package com.example.cs4.config;

import org.apache.catalina.connector.Connector;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@Configuration
public class MultiPortConfig {

    @Bean
    public ServletWebServerFactory servletContainer(Environment env) {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory(Integer.parseInt(env.getProperty("server.port")));
        String[] additionalPorts = env.getProperty("server.additionalPorts", "").split(",");
        for (String port : additionalPorts) {
            Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); // 直接使用协议字符串
            connector.setPort(Integer.parseInt(port.trim()));
            tomcat.addAdditionalTomcatConnectors(connector);
        }
        return tomcat;
    }
}